﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MxWeiXinPF.Common;
using MxWeiXinPF.DBUtility;

namespace MxWeiXinPF.WeiXinComm
{
    public class OrderActiveSend
    {
        private WeiXinSendMessageComm wxSM = new WeiXinSendMessageComm();
        private readonly Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
        private string sendmobile = "";
        private string sendname = "";
        private string senduserid = "";

        public string WXSendMessage(int id)
        {
            string str11;
            BLL.orders border = new BLL.orders();
            DataSet set = border.SelectOrderInfo(id);
            int wid = int.Parse(set.Tables[0].Rows[0]["wid"].ToString());
            int shopid = int.Parse(set.Tables[0].Rows[0]["shopid"].ToString());
            string openid = set.Tables[0].Rows[0]["openid"].ToString();
            string str2 = set.Tables[0].Rows[0]["pwxopenid"].ToString();
            string str3 = set.Tables[0].Rows[0]["ppwxopenid"].ToString();
            decimal amount = decimal.Parse(set.Tables[0].Rows[0]["amount"].ToString());
            decimal num4 = decimal.Parse(set.Tables[0].Rows[0]["pamount"].ToString());
            decimal num5 = decimal.Parse(set.Tables[0].Rows[0]["ppamount"].ToString());
            decimal sumamount = decimal.Parse(set.Tables[0].Rows[0]["sumamount"].ToString());
            decimal num7 = decimal.Parse(set.Tables[0].Rows[0]["psumamount"].ToString());
            decimal num8 = decimal.Parse(set.Tables[0].Rows[0]["ppsumamount"].ToString());
            string str4 = set.Tables[0].Rows[0]["nick_name"].ToString();
            string str5 = set.Tables[0].Rows[0]["pnick_name"].ToString();
            string str6 = set.Tables[0].Rows[0]["ppnick_name"].ToString();
            string phone = set.Tables[0].Rows[0]["mobile"].ToString();
            string str8 = set.Tables[0].Rows[0]["pmobile"].ToString();
            string str9 = set.Tables[0].Rows[0]["ppmobile"].ToString();
            string url = string.Concat(new object[] { MyCommFun.getWebSite(), "/wxoauth.aspx?wid=", wid, "&p=6" });
            if ((openid != null) && !openid.Equals(""))
            {
                if (amount > 0M)
                {
                    this.wxSM.SendMessageShouHuo("1", openid, wid, amount, sumamount, "", "", url);
                }
                else if (amount < 0M)
                {
                    this.wxSM.SendMessageShouHuo("2", openid, wid, amount, sumamount, "", "", url);
                }
            }
            else if (((phone != null) && !phone.Equals("")) && border.isAllowSend(shopid))
            {
                str11 = string.Concat(new object[] { "尊敬的", str4, "您本次购物获得积分：", amount, "分" });
                this.SendSMSMessage(id, shopid, phone, str4, str11);
            }
            if ((str2 != null) && !str2.Equals(""))
            {
                if (num4 > 0M)
                {
                    this.wxSM.SendMessageShouHuo("1", str2, wid, num4, num7, str4, "", url);
                }
                else if (num4 < 0M)
                {
                    this.wxSM.SendMessageShouHuo("2", str2, wid, num4, num7, str4, "", url);
                }
            }
            else if (((str8 != null) && !str8.Equals("")) && border.isAllowSend(shopid))
            {
                str11 = string.Concat(new object[] { "尊敬的", str5, "您邀请的", str4, "完成交易您获得积分：", num4, "分" });
                this.SendSMSMessage(id, shopid, str8, str5, str11);
            }
            if ((str3 != null) && !str3.Equals(""))
            {
                if (num5 > 0M)
                {
                    this.wxSM.SendMessageShouHuo("1", str3, wid, num5, num8, str5, str4, url);
                }
                else if (num5 < 0M)
                {
                    this.wxSM.SendMessageShouHuo("2", str3, wid, num5, num8, str5, str4, url);
                }
            }
            else if (((str9 != null) && !str9.Equals("")) && border.isAllowSend(shopid))
            {
                str11 = string.Concat(new object[] { "您邀请的", str5, "的下级", str4, "完成交易您获得积分：", num5, "分" });
                this.SendSMSMessage(id, shopid, str9, str6, str11);
            }
            return "";
        }

        public void SendMessage(int id)
        {
            BLL.orders border = new BLL.orders();
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 id,order_no,trade_no,user_id,user_name,payment_id,payment_fee,payment_status,payment_time,express_id,express_no,express_fee,express_status,express_time,accept_name,post_code,telphone,mobile,area,address,message,remark,payable_amount,real_amount,order_amount,point,status,add_time,confirm_time,complete_time,wid,openid,modelName,modelCode,modelActionName,modelActionId,orderSubject,city,district,notify_id,pay_info,isSubscribe,fahuoCode,fahuoMsg,shopid,parentid,rate_amount ");
            builder.Append(" from " + siteConfig.sysdatabaseprefix + "orders ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            this.sendname = set.Tables[0].Rows[0]["accept_name"].ToString();
            this.sendmobile = set.Tables[0].Rows[0]["mobile"].ToString();
            this.senduserid = set.Tables[0].Rows[0]["user_id"].ToString();
            if (this.sendmobile.Equals(""))
            {
                this.sendmobile = border.GetUserMobileByID(int.Parse(this.senduserid));
            }
            string s = set.Tables[0].Rows[0]["shopid"].ToString();
            string sendmobile = this.sendmobile;
            string sendname = this.sendname;
            string messageContent = border.GetMessageContent(id, int.Parse(this.senduserid));
            string contentTxt = "尊敬的" + this.sendname + "您本次购物获得积分：" + messageContent + "分";
            string htmlFromUrl = border.GetHtmlFromUrl(sendmobile, contentTxt);
            if (!(string.IsNullOrEmpty(htmlFromUrl) || htmlFromUrl.Equals("")))
            {
                int num = border.AddInfo(this.sendname, this.sendmobile, id, contentTxt, "1", 0x27, int.Parse(s));
            }
        }

        public void SendSMSMessage(int id, int shopid, string phone, string nick_name, string contenttxt)
        {
            BLL.orders border = new BLL.orders();
            string htmlFromUrl = border.GetHtmlFromUrl(phone, contenttxt);
            if (!(string.IsNullOrEmpty(htmlFromUrl) || htmlFromUrl.Equals("")))
            {
                int num = border.AddInfo(nick_name, phone, id, contenttxt, "1", 0x27, shopid);
            }
        }
    }
}
